package com.food.foodapi.instans;

public class Result<T> {
    private Integer code;
    private String message;
    private T data;

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 成功的响应（带数据）
    public static <T> Result<T> success(T data) {
        return new Result<>(200, "请求成功", data);
    }

    // 成功的响应（带消息）
    public static <T> Result<T> success(String message, T data) {
        return new Result<>(200, message, data);
    }

    // 成功的响应（不带数据）
    public static <T> Result<T> success() {
        return new Result<>(200, "请求成功", null);
    }

    // 失败的响应（带状态码和消息）
    public static <T> Result<T> error(Integer code, String message) {
        return new Result<>(code, message, null);
    }

    // 失败的响应（带消息，默认500状态码）
    public static <T> Result<T> error(String message) {
        return new Result<>(500, message, null);
    }

    // 参数校验失败
    public static <T> Result<T> badRequest(String message) {
        return new Result<>(400, message, null);
    }

    // 服务器错误响应
    public static <T> Result<T> internalError(String message) {
        return new Result<>(500, message, null);
    }

    // 未授权
    public static <T> Result<T> unauthorized(String message) {
        return new Result<>(401, message, null);
    }

    // 未找到
    public static <T> Result<T> notFound(String message) {
        return new Result<>(404, message, null);
    }

    // 响应消息的可链式调用
    public Result<T> code(Integer code) {
        this.code = code;
        return this;
    }

    public Result<T> message(String message) {
        this.message = message;
        return this;
    }

    public Result<T> data(T data) {
        this.data = data;
        return this;
    }

    // getters and setters

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    // toString method to print the object

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
